



#include "gps.h"

char GPSDataBuf[MAX_GPSSTR_LEN]; //buffer for gps data

unsigned char GpsBufIdx=0;
volatile unsigned char updateGPSData=0;

char GPSData[GPS_NUM_TYPES][MAX_GPSSTR_LEN]; //most recent data
char GpsTag[GPS_TAG_LEN+1];
unsigned char GpsIdx=0;
unsigned char GpsType=0; //represents the type of gps data string we're currently processing

//returns 1 if str1 == str2
unsigned char strCmp(char *str1, char *str2){
  unsigned char idx=0;
  unsigned char same=1;
  unsigned char done=0;

  while(same && !done){
    if(str1[idx] != str2[idx]) { same = 0; }
    if(str1[idx] == 0 || str2[idx] == 0) { done = 1; }
    idx += 1;
  }
  return same;
}

//updates gps data from buffer
void parseGpsData(void){
  unsigned char i;
  while(updateGPSData) { //updateGPSData set by ISR and waits for it to be 0 before refilling buffer
    
    //GpsIdx will tell use what phase of data capture we're in
    if(GpsIdx >= MAX_GPSSTR_LEN) {  //error
      updateGPSData = 0;
      GpsIdx = 0;
    }
    else if(GpsIdx == 0){ //initial state
      if(GPSDataBuf[GpsIdx] == '$'){ //start token
        GpsTag[GpsIdx] = GPSDataBuf[GpsIdx];
        GpsIdx += 1;
      } 
      else{
        updateGPSData = 0; 
      }
    }    
    else if(GpsIdx > 0 && GpsIdx < GPS_TAG_LEN){ //construct GPS tag
      GpsTag[GpsIdx] = GPSDataBuf[GpsIdx];
      GpsIdx += 1;
    }
    else if(GpsIdx == GPS_TAG_LEN){ //by this time we have the full GPS tag and can begin filling the correct buffer
      GpsTag[GpsIdx] = 0; //complete the string
      if( strCmp(GpsTag, TAG_GPGGA) ) {
        GpsType = TYPE_GPGGA; 
      }
      else if( strCmp(GpsTag, TAG_GPRMC) ) { 
        GpsType = TYPE_GPRMC;
      }
      else if( strCmp(GpsTag, TAG_GPGSA) ) {
        GpsType = TYPE_GPGSA;
      } 
      else { //not a tag we care about
        GpsIdx = 0; 
        updateGPSData = 0;   
      }
    
      if(GpsIdx != 0) { //if we found a tag we want
        GpsIdx += 1;
      } 
    }
    else if(GPSDataBuf[GpsIdx] != LF){
      GpsIdx += 1;
    }
    else{ //done with string
      for(i=0; i<=GpsIdx; i++){ //copy buffer into data var
        GPSData[GpsType][i] = GPSDataBuf[i];
      }
      GPSData[GpsType][GpsIdx+1] = 0;
      GpsIdx = 0;
      updateGPSData = 0;
    }
  }
}

void GPS_BufferData(char data){
    if(updateGPSData) { //not done reading from buffer
    	return;
    } 
    else {  //may need to look for start token...
      GPSDataBuf[GpsBufIdx] = data; //buffer for gps data
      if(GPSDataBuf[GpsBufIdx] == LF) { 
        updateGPSData = 1; 
        GpsBufIdx = 0;
      } 
      else{
        GpsBufIdx += 1;  
      }
    }
    
     
}

unsigned char GPS_LastParsedType(void){
	return GpsType;
}


unsigned char GPS_getUpdateFlag(void){
  return updateGPSData; 
}

void GPS_setUpdateFlag(){
  updateGPSData = 1;
}

char ** GPS_getDataPtr(void){
  return GPSData; 
}

char ** GPS_BufferPtr(void){
  return GPSDataBuf; 
}

//char 
//$PSRF100,1,38400,8,1,0*3D
